﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using SMBCFamilyAssessment.Models;
using SMBCFamilyAssessment.Logic;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
namespace SMBCFamilyAssessment.Logic
{
    public class RoleActions
    {
        internal void createAdmin()
        {
          
            //Access App context and create result variables

            using (var context = new ApplicationDbContext())
            
            {


                IdentityResult IdRoleresult;
                IdentityResult IdUserResult;


                // Create a RoleStore object by using the ApplicationDbContext object.
                // The RoleStore is only allowed to contain IdentityRole objects. 
                var roleStore = new RoleStore<IdentityRole>(context);



                // Create a RoleManager object that is only allowed to contain IdentityRole objects.
                // When creating the RoleManager object, you pass in (as a parameter) a new RoleStore object. 
                var roleMgr = new RoleManager<IdentityRole>(roleStore);

                //create administrator role if it doesn't already exists

                if (!roleMgr.RoleExists("Administrator"))
                {

                    IdRoleresult = roleMgr.Create(new IdentityRole("Administrator"));
                    if (!IdRoleresult.Succeeded)
                    {
                        // Handle the error condition if there's a problem creating the RoleManager object.

                    }

                }

                // Create a UserManager object based on the UserStore object and the ApplicationDbContext 
                // object. Note that you can create new objects and use them as parameters in 
                // a single line of code, rather than using multiple lines of code, as you did 
                // for the RoleManager object. 
                var userMgr = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));

                var appUser = new ApplicationUser()
                {
                    UserName = "atifmir@solihull.gov.uk",
                    Email = "atifmir@solihull.gov.uk"


                };

                IdUserResult = userMgr.Create(appUser, "$mbc.123");
                // If the new "Admin" user was successfully created, 
                // add the "Admin" user to the "Administrator" role.
                if (IdUserResult.Succeeded)
                {
                    IdUserResult = userMgr.AddToRole(appUser.Id, "Administrator");


                    if (!IdUserResult.Succeeded)
                    { 
                        // Handle the error condition if there's a problem adding the user to the role. 
                    }

                }
                else
                {

                    // Handle the error condition if there's a problem creating the new user.

                }


            }



        }
    }
}